﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ATA.OLSD.ScoringService.Entities;

namespace CPAScoringPerformanceTester.TestTask
{
    public class ScoreTaskApplyTask : TestTaskBase
    {
        private UserAccessTicket ticket;
        private PersonTask task;
        private ScoringItem[] items;
        public static string TaskName = "Score Task Apply Test";

        public ScoreTaskApplyTask(UserAccessTicket ticket, PersonTask task, ScoringItem[] items)
        {
            this.ticket = ticket;
            this.task = task;
            this.items = items;
        }

        public override void Setup()
        {
            base.Setup();

            if (!ScoreTaskDetailSettings.Instance.TaskId.HasValue)
            {
                Message = ERROR_TASK_NOT_SPECIFIED;
                IsError = true;
                return;
            }

            if (ScoreTaskApplySettings.Instance.ApplyCount <= 0)
            {
                Message = "Count of application must be larger than zero";
                IsError = true;
                return;
            }

            if (ticket == null)
            {
                Message = ERROR_BAD_ACCOUNT;
                IsError = true;
                return;
            }

            if (task == null)
            {
                Message = "Get score task detail first";
                IsError = true;
                return;
            }

            if (task.PersonDistributedCount > 0)
            {
                Message = "Cannot apply repeatedly";
                IsError = true;
                return;
            }
            else if (task.PersonAvailableCount <= 0)
            {
                Message = "No pending response";
                IsError = true;
                return;
            }

            if (items != null && !ScoreTaskApplySettings.Instance.NoPriority)
            {
                Random random = new Random(Guid.NewGuid().GetHashCode());
                foreach (var item in items)
                {
                    if (ScoreTaskApplySettings.Instance.AllPriority)
                    {
                        item.Priority = true;
                    }
                    else if (ScoreTaskApplySettings.Instance.RandomPriority)
                    {
                        if (random.Next() % 2 == 0) item.Priority = true;
                    }
                }
            }
        }

        public override bool Execute()
        {
            try
            {
                Client = ServiceProxy.NewProxy();
                int count = Client.ApplyPersonTask(ticket, new ScoringRequestContext()
                {
                    Type = TaskType.Scoring,
                    TaskId = ScoreTaskDetailSettings.Instance.TaskId.Value
                }, ScoreTaskApplySettings.Instance.ApplyCount, items == null ? null : items.ToArray());
                return count > 0;
            }
            catch (Exception ex)
            {
                Message = ex.Message;
                return false;
            }
            finally
            {
                Client.CloseConnection();
            }
        }
    }
}
